#include "head.h"

int func(union u_ u)
{
	return u.b;
}

void select_sort(int array[], int n)
{
	int i,j,k,temp;
	for(i = 0; i < n-1; ++i)
	{
		k = i;
		for(j = i+1; j < n; ++j)
		{
			if(array[j] < array[k])
			{
				k = j;
			}
		}

		temp = array[i];
		array[i] = array[k];
		array[k] = temp;
	}
}

void array_reverse1(int arr[], int n)
{
	
	int i,j,temp;
	int k = (int)sizeof(arr);
	assert(n>0);
	for(i = 0, j = n-1; i < j; i++, j--)
	{
		temp = arr[i];
		arr[i] = arr[j];
		arr[j] = temp;
	}
}
void array_reverse2(int *arr, int n)
{
	
	int *i,*j,temp;
	int k = (int)sizeof(arr);
	assert(n>0);
	for(i = arr, j = arr + n - 1; i < j; i++, j--)
	{
		temp = *i;
		*i = *j;
		*j = temp;
	}
}

void copy_string(char *dest, char *src)
{
	assert(dest != 0 && src != 0);
	while(*dest++ = *src++);
	return;	
}

LinkedNode *create_single_list(void)
{
	LinkedNode *head, *temp_head, *temp_temp_head, *temp, *temp1;

	temp = (LinkedNode *)malloc(sizeof(LinkedNode));
	if(NULL == temp)
	{
		return NULL;
	}
	head = temp;
	temp_head = head;
	head->link = NULL;
	printf("Please entry the number and score:\n");
	scanf("%d%f", &temp->number, &temp->score);
	
	do
	{
		temp1 = (LinkedNode *)malloc(sizeof(LinkedNode));
		if(NULL == temp1)
		{
			return NULL;
		}
		temp_head->link = temp1;
		temp_temp_head = temp_head;
		temp_head = temp1;
		printf("Please entry the number and score:\n");
	}
	while(scanf("%d%f", &temp1->number, &temp1->score) != EOF);

	free(temp_head);
	temp_head = NULL;
	temp_temp_head->link = NULL;
	printf("Single list created!\n");

	return head;

}

void destory_single_list(LinkedNode *head)
{
	LinkedNode *temp;
	if(NULL == head)
	{
		printf("The single list is not existent.\n");
		return;
	}

	do
	{
		temp = head;
		head = head->link;
		free(temp);
		temp = NULL;
	}
	while(NULL != head);
	printf("Single list deleted\n");
}

void print_single_list(LinkedNode *head)
{
	//The commented codes are to make sure that the printed nodes are no more than 10.
	int counter = 0;
	printf("Now printing the list...\n");
	if(NULL == head)
	{
		printf("The single list is not existent.\n");
	}
	while(NULL != head)
	{
		printf("%d, %f\n", head->number, head->score);
		counter++;
		if(counter > 10)
		{
			return;
		}
		head = head->link;
	}

	printf("Print finished.\n");
	return;
}

LinkedNode *delete_one_node_from_single_list(LinkedNode *head, int num)
{
	LinkedNode *temp1 = head, *temp2;
	
	if(NULL == head)
	{
		printf("The single list is not existent.\n");
		return NULL;
	}

	if(head->number == num)
	{
		head = head->link;
		free(temp1);
		temp1 = NULL;
		printf("Deleted the first node.\n");
		return head;
	}

	temp2 = temp1;
	temp1 = temp1->link;
	while(NULL != temp1)
	{
		if(num == temp1->number)
		{
			temp2->link = temp1->link;
			free(temp1);
			temp1 = NULL;
			printf("Deleted one node.\n");
			return head;
		}
		temp2 = temp1;
		temp1 = temp1->link;
	}

	printf("Can not find the node.\n");
	return head;
}

LinkedNode *add_one_node_to_single_list(LinkedNode *head, LinkedNode *node)
{
	LinkedNode *temp1, *temp2, *temp3;
	if(NULL == head || NULL == node)
	{
		return head;
	}

	if(node->number <= head->number)
	{
		node->link = head;
		return node;
	}
	
	temp1 = head->link;
	temp3 = head;
	temp2 = node;

	while(NULL != temp1)
	{
		if(temp2->number > temp1->number)
		{
			temp3 = temp1;
			temp1 = temp1->link;
		}
		else
		{
			temp2->link = temp1;
			temp3->link = temp2;
			return head;
		}
	}

	//If can not find the place to insert, append it to the end of the single list.
	temp3->link = temp2;
	temp2->link = NULL;
	return head;
	
}

void swap_xor(int *a, int *b)
{
	*a = *a^*b;
	*b = *a^*b;
	*a = *a^*b;	
}

void swap_plus_minus(int *a, int *b)
{
	*a = *a+*b;
	*b = *a-*b;
	*a = *a-*b;
}

int cut_bit4_to_bit7(int x)
{
	return x >> 4 & ~(~0 << 4);
}

int cycle_move_right(int x, int n)
{
	int temp;
	temp = x << sizeof(int)*8-n;
	//x >>= n;
	return x >> n | temp;
}

int partition(int array[], int low, int high)
{
	int axis = array[low];
	while(low < high)
	{
		while(low < high && axis <= array[high])
		{
			high--;
		}
		swap_temp(&array[low], &array[high]);

		while(low < high && axis >= array[low])
		{
			low++;
		}
		swap_temp(&array[low], &array[high]);
		
	}

	if(low == high)
	{
		return axis = high;
	}

	return -1;
}
void quick_sort(int array[], int low, int high)
{
	int index;
	if(low < high)
	{
		index = partition(array, low, high);
		quick_sort(array, low, index-1);
		quick_sort(array, index+1, high);
	}
}

void swap_temp(int *a, int *b)
{
	int temp;
	temp = *a;
	*a = *b;
	*b = temp;
}